﻿using System.Windows;
using System.Windows.Controls;
using Dispatcher.Views;
using Repository;
using Dispatcher.Extensions;

namespace Dispatcher.TemplateSelectors
{
    /// <summary>
    /// Event Template Selector.
    /// </summary>
    internal class EventTemplateSelector : DataTemplateSelector
    {
        /// <summary>
        /// When overridden in a derived class, returns a <see cref="T:System.Windows.DataTemplate"/> based on custom logic.
        /// </summary>
        /// <param name="item">The data object for which to select the template.</param>
        /// <param name="container">The data-bound object.</param>
        /// <returns>
        /// Returns a <see cref="T:System.Windows.DataTemplate"/> or null. The default value is null.
        /// </returns>
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            var element = container.GetNearestVisualParent<PlaceTimetableView>();

            if (element != null && item != null && item is Event)
            {
                var e = item as Event;

                if (e.Id > 0)
                {
                    return element.FindResource("BusyEventTemplate") as DataTemplate;
                }

                return element.FindResource("FreeEventTemplate") as DataTemplate;
            }

            return null;
        }
    }
}
